<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-tabs :activeKey="superParam.code" @change="tabChange">
        <a-tab-pane v-for="(item, index) in dictOptions" :key="item.value" :tab="item.text"> </a-tab-pane>
      </a-tabs>
      <a-form layout="inline" @keyup.enter.native="searchList">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="机构名称">
              <j-select-depart
                v-model="queryParam.officeId"
                :trigger-change="true"
                customReturnField="id"
                :multi="false"
              ></j-select-depart>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="年度">
              <a-date-picker
                mode="year"
                placeholder="请选择年份"
                format="YYYY"
                value-format="yyyy-MM-dd"
                v-model="queryParam.year"
                :open="yearPickShow"
                style="width: 100%"
                @panelChange="handlePanelChange"
                @openChange="handleOpenChange"
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="季度">
              <j-dict-select-tag
                style="width: 100%"
                v-model="queryParam.quarter"
                placeholder="请选择"
                dict-code="sys_quarter"
              />
            </a-form-item>
          </a-col>
          <!--            <a-form-item label="类型" class="bbsbtype">-->
          <!--              <j-dict-select-tag-->
          <!--                :showDefaultSelect="false"-->
          <!--                style="width: 100%"-->
          <!--                v-model="superParam.code"-->
          <!--                placeholder="请选择"-->
          <!--                dict-code="report_tab_menu"-->
          <!--              />-->
          <!--            </a-form-item>-->
          <!--          <template v-if="toggleSearchStatus">-->
          <!--            <a-col :md="6" :sm="8">-->
          <!--              <a-form-item label="季度">-->
          <!--                <j-dict-select-tag-->
          <!--                  style="width: 100%"-->
          <!--                  v-model="queryParam.quarter"-->
          <!--                  placeholder="请选择"-->
          <!--                  dict-code="sys_quarter"-->
          <!--                />-->
          <!--              </a-form-item>-->
          <!--            </a-col>-->
          <!--          </template>-->
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchList" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
              <a-button
                v-show="!(superParam.code == '7' || superParam.code == '6')"
                type="primary"
                style="
                  padding: 0 10px;
                  margin-left: 10px;
                  background-color: #1ea489 !important;
                  border: 1px solid #1ea489 !important;
                "
                @click="mergeReports"
                >合并报表</a-button
              >
              <!--              <a @click="handleToggleSearch" style="margin-left: 8px">-->
              <!--                {{ toggleSearchStatus ? '收起' : '展开' }}-->
              <!--                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>-->
              <!--              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        v-if="superParam.code == '6'"
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columnsTransfer"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="departName" slot-scope="departName">
          <div style="width: 100%; height: 35px; line-height: 35px; overflow-x: hidden; overflow-y: auto">
            {{ departName }}
          </div>
        </template>
        <span slot="actionName">
          <a-button size="small" @click="openCreatModel" v-has="'report:add'" type="primary" icon="plus">新增</a-button>
        </span>
        <span slot="create" slot-scope="text, record">
          {{ record.createByText || record.realName }}
        </span>
        <span slot="ifstate" slot-scope="text, record">
          {{ record.status_dictText || record.isMerge_dictText }}
        </span>
        <span slot="applystate" slot-scope="text, record">
          {{ record.applystate_dictText || record.state_dictText }}
        </span>
        <span slot="action" slot-scope="text, record">
          <span v-has="'report:detail'">
            <a @click="handleCheckData(record)">查看</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical" />
            <a @click="handleEditData(record)">编辑</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDeleteData(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
          <span v-has="'report:back'">
            <span v-if="record.applystate === 1 || record.state === 3">
              <a-divider type="vertical" />
              <a-popconfirm title="确定退回吗?" @confirm="() => handleSendBack(record)">
                <a>退回</a>
              </a-popconfirm>
            </span>
          </span>
          <!-- <span v-has="'report:export'">
            <a-divider type="vertical" />
            <a @click="handleDownload(record)">下载</a>
          </span> -->
        </span>
      </a-table>
      <a-table
        v-else-if="superParam.code == '7'"
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columnsTransfer"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="departName" slot-scope="departName">
          <div style="width: 100%; height: 35px; line-height: 35px; overflow-x: hidden; overflow-y: auto">
            {{ departName }}
          </div>
        </template>
        <span slot="actionName">
          <a-button size="small" @click="openCreatModel" v-has="'report:add'" type="primary" icon="plus">新增</a-button>
        </span>
        <span slot="create" slot-scope="text, record">
          {{ record.createByText || record.realName }}
        </span>
        <span slot="ifstate" slot-scope="text, record">
          {{ record.status_dictText || record.isMerge_dictText }}
        </span>
        <span slot="applystate" slot-scope="text, record">
          {{ record.applystate_dictText || record.state_dictText }}
        </span>
        <span slot="action" slot-scope="text, record">
          <span v-has="'report:detail'">
            <a @click="handleCheckData(record)">查看</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical" />
            <a @click="handleEditData(record)">编辑</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDeleteData(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
          <span v-has="'report:back'">
            <span v-if="record.applystate === 1 || record.state === 3">
              <a-divider type="vertical" />
              <a-popconfirm title="确定退回吗?" @confirm="() => handleSendBack(record)">
                <a>退回</a>
              </a-popconfirm>
            </span>
          </span>
          <span v-has="'report:export'">
            <a-divider type="vertical" />
            <a @click="handleDownload(record)">下载</a>
          </span>
        </span>
      </a-table>
      <a-table
        v-else
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :row-selection="rowSelection"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="departName" slot-scope="departName">
          <div style="width: 100%; height: 35px; line-height: 35px; overflow-x: hidden; overflow-y: auto">
            {{ departName }}
          </div>
        </template>
        <span slot="actionName">
          <a-button size="small" @click="openCreatModel" v-has="'report:add'" type="primary" icon="plus">新增</a-button>
        </span>
        <span slot="create" slot-scope="text, record">
          {{ record.createByText || record.realName }}
        </span>
        <span slot="ifstate" slot-scope="text, record">
          {{ record.status_dictText || record.isMerge_dictText }}
        </span>
        <span slot="applystate" slot-scope="text, record">
          {{ record.applystate_dictText || record.state_dictText }}
        </span>
        <span slot="action" slot-scope="text, record">
          <span v-has="'report:detail'">
            <a @click="handleCheckData(record)">查看</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical" />
            <a @click="handleEditData(record)">编辑</a>
          </span>
          <span v-has="'report:export'">
            <a-divider type="vertical" />
            <a @click="handleDownload(record)">下载</a>
          </span>
          <span v-if="record.status == 2 || record.isMerge == 1">
            <a-divider type="vertical" />
            <a>已合并</a>
          </span>
          <span v-if="banBtn(record)" v-has="'report:edit'">
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDeleteData(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
          <span v-has="'report:back'">
            <span v-if="record.applystate === 1 || record.state === 3">
              <a-divider type="vertical" />
              <a-popconfirm v-if="superParam.code != '2'" title="确定退回吗?" @confirm="() => handleSendBack(record)">
                <a>退回</a>
              </a-popconfirm>
              <a-popconfirm
                v-else
                title="退回后请重新创建，同年同季度的项目实施报表！"
                @confirm="() => handleSendBack(record)"
              >
                <a>退回</a>
              </a-popconfirm>
            </span>
          </span>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <creatReport ref="creatModel" @openModelView="handleAddData"></creatReport>
    <component v-bind:is="ModelView" ref="modalForm" @ok="updateProList"></component>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import store from '@/store/'
import ModelView1 from './ModelView/ModelView1'
import ModelView2 from './ModelView/ModelView2'
import ModelView3 from './ModelView/ModelView3'
import ModelView4 from './ModelView/ModelView4'
import ModelView5 from './ModelView/ModelView5'
import ModelView6 from './ModelView/ModelView6'
import ModelView7 from './ModelView/ModelView7'
import ModelView8 from './ModelView/ModelView8'
import ModelView9 from './ModelView/ModelView9'
import ModelView11 from './ModelView/ModelView11'
import ModelView10 from './ModelView/ModelView10'
import { getAction } from '@/api/manage'
import moment from 'moment'
import 'moment/locale/zh-cn'
import columns from './indexColumns'
import columnsTransfer from './columnsTransfer'
import creatReport from './creatReport'
import { getApi } from '@/utils/neiORwai'
import { ajaxGetDictItems } from '@/api/api'

export default {
  name: 'bbsb',
  mixins: [JeecgListMixin],
  components: {
    creatReport,
    ModelView1,
    ModelView2,
    ModelView3,
    ModelView4,
    ModelView5,
    ModelView6,
    ModelView7,
    ModelView9,
    ModelView8,
    ModelView11,
    ModelView10,
  },
  data() {
    return {
      dictOptions: [],
      queryParam: {
        officeId: '',
        year: '',
        code: '',
        quarter: '',
      },
      superParam: {
        type: '1',
        code: '1',
        sjType: '',
      },
      // 表头
      columns: columns(this),
      columnsTransfer: columnsTransfer(this),
      url: {
        list: '/sjxxzhgl/sjProjectReport/queryProjectInfoReport',
        downLoad: '/sjxxzhgl/sjProjectReport/exportProjectReportInfo',
        merge: '/sjxxzhgl/sjProjectReport/generateReport',
        back: '/common/sjProcCommon/backReport',
        back1: '/common/sjProcCommon/backProReport',
      },
      yearPickShow: false,
      banEdit: true,
      title: '审计项目实施情况统计表(季报)',
      modelNum: '1',
      reportIds: [],
      departInfo: store.getters.departInfo,
      disableMixinCreated: true,
    }
  },
  computed: {
    ModelView: function () {
      return 'ModelView' + this.modelNum
    },
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          this.reportIds = []
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
          // selectedRows.map((item) => {
          //   this.reportIds.push(item.id)
          // })
          this.reportIds = [...selectedRowKeys]
        },
      }
    },
  },
  created() {
    this.tabsData()
    this.searchList()
  },
  watch: {
    modelNum(val) {
      switch (val) {
        case '1':
          this.title = '审计项目实施情况统计表(季报)'
          break
        case '2':
          this.title = '审计发现问题分类统计表(季报)'
          break
        case '3':
          this.title = '单位情况信息表(季报)'
          break
        case '4':
          this.title = '总审计师与内部审计机构基本情况表(季报)'
          break
        case '5':
          this.title = '内部审计人员配备情况表(季报)'
          break
        case '6':
          this.title = '抽调审计人员费用统计表'
          break
        case '7':
          this.title = '单位及内部审计机构基本情况表'
          break
        case '8':
          this.title = '单位内部审计业务情况表'
          break
        case '9':
          this.title = '审计署报送报表情况统计'
          break
        case '10':
          this.title = '中央企业内部审计工作情况统计表'
          break
        case '11':
          this.title = '中央企业内部审计发现问题汇总表'
          break

        default:
          break
      }
    },
    'superParam.code': {
      handler: function (val) {
        if (val) {
          this.searchList()
        }
      },
    },
  },
  methods: {
    tabChange(activeKey) {
      this.superParam.code = activeKey
    },
    tabsData() {
      ajaxGetDictItems('report_tab_menu', null).then((res) => {
        if (res.success) {
          this.dictOptions = res.result
        }
      })
    },
    // 查询条件
    searchList() {
      this.reportIds = []
      if (this.superParam.code == '1' || this.superParam.code == '2') {
        this.superParam.type = this.superParam.code
        this.url.list = '/sjxxzhgl/sjProjectReport/queryProjectInfoReport'
      } else if (this.superParam.code == '6') {
        this.superParam.sjType = '4'
        this.url.list = '/sjxxzhbb/sjAuditInfo/queryAuditoCostReportInfo'
      } else if (this.superParam.code == '7') {
        this.superParam.sjType = '5'
        this.url.list = '/sjxxzhbb/sjAuditInfo/queryAuditorSJSOneInfo'
      } else if (this.superParam.code == '8') {
        this.superParam.sjType = '6'
        this.url.list = '/sjxxzhbb/sjAuditInfo/querySJSTwoReportInfo'
      } else if (this.superParam.code == '9') {
        this.superParam.sjType = '7'
        this.url.list = '/sjxxzhbb/sjsbb/queryBsbbqktjInfo'
      } else if (this.superParam.code == '10') {
        this.superParam.sjType = '8'
        this.url.list = '/sjxxzhbb/sjAuditInfo/queryGZWOneReportInfo'
      } else if (this.superParam.code == '11') {
        this.superParam.sjType = '9'
        this.url.list = '/sjxxzhbb/gzwbb/queryNbsjfxwthzInfo'
      } else {
        this.superParam.sjType = String(Number(this.superParam.code) - 2)
        this.url.list = '/sjxxzhbb/sjAuditInfo/queryAuditorReportInfo'
      }
      this.modelNum = this.superParam.code
      this.loadData(1)
    },
    searchReset() {
      this.queryParam = this.$options.data().queryParam
      this.loadData(1)
    },
    // 年份选择器 begin
    moment,
    // 得到年份选择器的值
    handlePanelChange(value) {
      this.yearPickShow = false
      this.queryParam.year = moment(value).format('YYYY')
    },
    // 弹出日历和关闭日历的回调
    handleOpenChange(status) {
      this.yearPickShow = status
      if(this.queryParam.year){
        this.queryParam.year = moment(this.queryParam.year.slice(0, 4)).format('YYYY')
      }
    },
    // 年份选择器 end

    // 新增弹窗
    openCreatModel() {
      this.$refs.creatModel.code = this.superParam.code
      this.$refs.creatModel.visible = true
    },

    // 操作按钮
    handleAddData: function (record) {
      // let sjType = ''
      // switch (this.superParam.code) {
      //   case '8':
      //     this.url.queryIsExist = '/sjxxzhbb/sjsbb/queryLastDwywqk'
      //     sjType = '6'
      //     break
      // }
      // let params = Object.assign({}, record, { officeId: this.departInfo.id, sjType, state: 3 })
      // getAction(this.url.queryIsExist, params).then((res) => {
      //   if (res.success) {
      //     this.$message.success(res.message)
      //   } else {
      //     this.$message.warning(res.message)
      //   }
      // })
      this.$refs.modalForm.add(record)
      this.$refs.modalForm.title = this.title
      this.$refs.modalForm.isCheck = false
      this.$refs.modalForm.disableSubmit = false
    },
    handleCheckData: function (record) {
      this.$refs.modalForm.check(record)
      this.$refs.modalForm.title = this.title
      this.$refs.modalForm.isCheck = true
      this.$refs.modalForm.disableSubmit = true
    },
    handleEditData: function (record) {
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = this.title
      this.$refs.modalForm.isCheck = false
      this.$refs.modalForm.disableSubmit = false
    },
    handleDeleteData: function (record) {
      this.$refs.modalForm.delete(record, () => {
        this.searchList()
      })
    },
    handleDownload: function (record) {
      let params = {}
      if (this.superParam.code == '1' || this.superParam.code == '2') {
        // 前两个
        this.url.downLoad = '/sjxxzhgl/sjProjectReport/exportProjectReportInfo'
        this.queryParam.type = this.superParam.code
      } else if (this.superParam.code == '6') {
        // 最后一个
        this.url.downLoad = '/sjxxzhbb/sjAuditSummaryInfo/exportAuditCostDetailInfo'
        this.queryParam.type = '4'
      } else if (this.superParam.code == '7') {
        //单位及内部审计机构基本情况表
        this.url.downLoad = '/sjxxzhbb/sjsbb/exportDwjnbsjjgbbqkb'
        this.queryParam.type = '5'
        params = {
          year: record.year,
          quarter: record.quarter,
          officeId: record.officeId,
        }
      } else if (this.superParam.code == '8') {
        //单位内部审计业务情况表
        this.url.downLoad = '/sjxxzhbb/sjsbb/exportDwywqkInfo'
        this.queryParam.type = '6'
        params = {
          year: record.year,
          quarter: record.quarter,
          officeId: record.officeId,
        }
      } else if (this.superParam.code == '9') {
        //审计署报送报表情况统计
        this.url.downLoad = '/sjxxzhbb/sjsbb/exportBsbbqktjInfo'
        this.queryParam.type = '7'
      } else if (this.superParam.code == '10') {
        //中央企业内部审计工作情况统计表
        this.url.downLoad = '/sjxxzhbb/sjsbb/exportNdzyqynbsjgzqktjInfo'
        this.queryParam.type = '8'
      } else if (this.superParam.code == '11') {
        //中央企业内部审计发现问题汇总表
        this.url.downLoad = '/sjxxzhbb/gzwbb/exportNbsjfxwthzInfo'
        this.queryParam.type = '9'
      } else {
        // 中间几个
        this.url.downLoad = '/sjxxzhbb/sjAuditInfo/exportAuditInfo'
        this.queryParam.type = String(Number(this.superParam.code) - 2)
      }
      getAction(
        this.url.downLoad,
        Object.assign({ id: record.id, type: this.queryParam.type, sjType: this.queryParam.type }, params)
      )
        .then((res) => {
          if (res.success && res.result) {
            this.$message.success('正在下载，请稍等！')
            let url = `${getApi() || window._CONFIG['domianURL']}/sys/common/static${res.result}`
            // let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
            window.open(url, '_blank')
          } else {
          }
        })
        .finally(() => {})
    },
    updateProList() {
      this.loadData()
    },
    banBtn(record) {
      if (this.modelNum == '1' || this.modelNum == '2') {
        if (record.applystate == 0 || record.applystate == 2) {
          return true
        } else {
          return false
        }
      } else {
        if (record.state == 0 || record.state == 2) {
          return true
        } else {
          return false
        }
      }
    },
    mergeReports() {
      this.loading = true
      if (this.superParam.code == '1' || this.superParam.code == '2') {
        this.url.merge = '/sjxxzhgl/sjProjectReport/generateReport'
      } else if (this.superParam.code == '8') {
        this.url.merge = '/sjxxzhbb/sjAuditInfo/generateSJSTwoReport'
      } else if (this.superParam.code == '9') {
        this.url.merge = '/sjxxzhbb/sjsbb/generateBsbbqktjReport'
      } else if (this.superParam.code == '11') {
        this.url.merge = '/sjxxzhbb/gzwbb/generateNbsjfxwthzReport'
      } else if (this.superParam.code == '10') {
        this.url.merge = '/sjxxzhbb/sjAuditInfo/generateAuditReport'
      } else {
        this.url.merge = '/sjxxzhbb/sjAuditInfo/generateReport'
      }
      getAction(this.url.merge, { ids: JSON.stringify(this.reportIds) })
        .then((res) => {
          this.loading = false
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {})
    },
    handleSendBack(record) {
      // 退回
      let url = this.url.back
      let type = this.superParam.sjType
      if (this.superParam.code === '1' || this.superParam.code === '2') {
        url = this.url.back1
        type = this.superParam.code
      }
      getAction(url, { type, proId: record.id, reason: '' }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';

.bbsbtype {
  /deep/ .ant-form-item-label {
    label::before {
      content: '* ';
      color: red;
    }
  }
}
</style>